import create from 'dd-store'
import pageStore from '/pages/stores/globalStore'
import { getIndexList } from '/pages/api/manage'
create.Page({
  store: pageStore,
  data: {
    size: 10,
    page: 1,
    hasMore: true,
    isLoading: true,
    selectedNumber: 0,
    // 全选
    isCheckAll: false,
    // 是否是搜索状态
    isSearch: false,
    // 是否是筛选状态
    isScreen: false,
    // 存放的全部数据
    indexDataList: [],
    // 要展示的数据
    // targetLibShowList: [],
    // 已选数据
    targetLibSelectedList: [],
    isShowTask: null,
    taskWeight: 0,
    mannerWeight: 0,
    sumWeight: 0,
    taskList: [],
    mannerList: [],
    totalityDimensionObj: [],
    // // 默认选择的数据
    // defaultSelectList:[]
  },
  onLoad() {
    this.loadForm(true, 1)

    this.computeSelectedNumber()
    // console.log(this.$store.data.isShowTask);

  },
  // 获取数据的函数
  loadForm(refresh = false, page) {
    dd.showLoading({
      content: '加载中...',
    });
    this.setData({
      isLoading: true
    })
    let data = {
      page,
      size: this.data.size,
    }
    getIndexList(data).then(rs => {
      // console.log(rs.data.list, 'rs');
      let newDataList = []
      refresh ? newDataList = rs.data.list : newDataList = this.data.indexDataList.concat(rs.data.list)
      const hasMore = newDataList.length >= this.data.size;
      dd.hideLoading();
      newDataList.forEach((item1) => {
        this.data.indexDataList.forEach((item2) => {
          if (!item1.targetLibId == item2.targetLibId) {
            item1.isSelected = false
          }
        })
      })
      newDataList.forEach((item1) => {
        this.$store.data.currentTargetList.forEach((item2) => {
          if (item1.targetLibName == item2.pmTargetName) {
            item1.isSelected = true
          }
        })
      })
      let isCheckAll = false
      let checkNum = 0
      newDataList.forEach((item) => {
        if (item.isSelected) {
          checkNum += 1
          console.log('选了');
        }
      })
      if (newDataList.length == checkNum) {
        isCheckAll = true
      }
      console.log();
      this.setData({
        indexDataList: newDataList,
        isLoading: false,
        hasMore,
        page,
        isCheckAll: isCheckAll
      })
    })
    // // 初始化的时候，将已经选择过的指标重新勾选上
    // this.defaultCheckFunc()
  },

  // 下拉刷新
  onReachBottom() {
    if (this.data.hasMore && !this.data.isLoading) {
      this.loadForm(false, this.data.page + 1)
    }
  },
  // 计算已选的函数
  computeSelectedNumber() {
    let number = 0
    this.data.indexDataList.forEach((item) => {
      if (item.isSelected) {
        number++
      }
    })
    this.setData({
      selectedNumber: number
    })
    // console.log(number);
  },
  // 全选的change事件
  checkAllChange(checked, e) {
    //  // console.log(checked);
    let selectList = []
    let newList = this.data.indexDataList
    if (!checked) {
      newList.forEach((item) => {
        item.isSelected = false
      })
    } else {
      newList.forEach((item) => {
        item.isSelected = true
      })
    }
    newList.forEach((item) => {
      if (item.isSelected) {
        selectList.push(item)
      }
    })
    this.setData({
      indexDataList: newList,
      isCheckAll: checked,
      targetLibSelectedList: selectList
    })
    this.computeSelectedNumber()
  },
  // 数据列表的change事件
  dataListChinage(checked, e) {
    let selectName = e.target.dataset.info.targetLibName
    console.log(checked, e);
    let newList = this.data.indexDataList
    if (checked) {
      newList.forEach((item) => {
        if (item.targetLibName == selectName) {
          item.isSelected = true
        }
      })
    } else {
      newList.forEach((item) => {
        if (item.targetLibName == selectName) {
          item.isSelected = false
        }
      })
    }

    this.setData({
      indexDataList: newList
    })
  },
  // 向store里面更新数据的函数
  updateStoreFunc() {
    // console.log(this.data.selectList);
    this.$store.data.currentTargetList = this.data.currentTargetList
    this.update()
    let pages = getCurrentPages();
    let prevPage = pages[pages.length - 2];
    prevPage.getStoreToTargetListFunc();
    prevPage.computedCuurentDimensionSumWeightFunc();
    prevPage.targetDataToDimensionDataFunc()
  },
  // 显示搜索
  showSearch() {
    this.setData({
      isSearch: true
    })
  },
  // 搜索的搜索框的取消按钮点击事件
  searchCancel() {
    this.setData({
      isSearch: false
    })
  },
  // 底部的取消按钮
  footerCancel() {
    dd.navigateBack({ data: 1 });
  },
  // 底部的确定按钮
  footerConfirm() {
    let selectedNameList = []
    // 将所有选中的指标添加到store里面的当前维度指标列表
    let newArr = this.data.indexDataList
    let newList = this.$store.data.currentTargetList
    newList.forEach((item) => {
      selectedNameList.push(item.pmTargetName)
    })
    console.log(selectedNameList);
    newArr.forEach((item) => {
      if (item.isSelected && (selectedNameList.indexOf(item.targetLibName) == -1)) {
        console.log('进来了');
        newList.push({
          makeProperty: 'add',
          pmTargetWeight: item.targetLibWeight,
          sort: 1,
          pmTargetName: item.targetLibName,
          pmTargetStand: item.targetLibStand
        })
      }
    })
    this.setData({
      currentTargetList: newList
    })
    console.log(newList);

    dd.navigateBack();
    this.updateStoreFunc()
  },
  // 根据store里面存储的当前维度状态，将选中的数据添加到对应的维度中去
  updateDimensionDataFunc() {
    // 选中为空
    if (this.data.targetLibSelectedList.length == 0) {
      let newArr = []
      let nullArr = []
      let newWeight = 0
      if (this.$store.data.isShowTask) {
        newArr = this.$store.data.taskList
        newArr.forEach((item) => {
          if (item.type != 'indexLibrary') {
            nullArr.push(item)
          }
        })
        // console.log(nullArr);
        // let weight = 0
        nullArr.forEach((item) => {
          newWeight += item.weight
        })
        console.log(newWeight);
        // console.log(newWeight);
        this.$store.data.taskList = nullArr
        this.$store.data.taskWeight = newWeight
        this.update()
      } else {
        newArr = this.$store.data.mannerList
        newArr.forEach((item) => {
          if (item.type != 'indexLibrary') {
            nullArr.push(item)
          }
        })
        console.log(nullArr);
        nullArr.forEach((item) => {
          newWeight += item.weight
        })
        console.log(newWeight);
        this.$store.data.mannerList = nullArr
        this.$store.data.mannerWeight = newWeight
        this.update()
        console.log(this.$store.data.mannerWeight);
      }
    }

    // 选中不为空
    let newArr = [];
    let weight = 0;
    this.data.targetLibSelectedList.forEach((item) => {
      newArr.push({
        indexName: item.targetLibName,
        criteriaOfAssessment: item.targetLibStand,
        weight: item.targetLibWeight,
        type: 'indexLibrary'
      })
    })
    newArr.forEach((item) => {
      weight += item.weight
    })
    if (this.$store.data.isShowTask) {
      let newList = []
      if (this.$store.data.taskList.length == 0) {
        newList = newArr
        this.$store.data.taskList = this.$store.data.taskList.concat(newList)
        this.$store.data.taskWeight += weight
      } else {
        let nameArr = []
        this.$store.data.taskList.forEach((item) => {
          nameArr.push(item.indexName)
        })
        let arr = []
        let sumweight = 0
        newArr.forEach((item) => {
          if (nameArr.indexOf(item.indexName) === -1) {
            arr.push(item)
            sumweight += item.weight
          }
        })
        let fistArr = this.$store.data.taskList.concat(arr)
        let sWeight = this.$store.data.taskWeight += sumweight
        this.$store.data.taskWeight = sWeight
        this.$store.data.taskList = fistArr
        this.update()
      }
    } else {
      let newList = []
      if (this.$store.data.mannerList.length == 0) {
        newList = newArr
        this.$store.data.mannerList = this.$store.data.mannerList.concat(newList)
        this.$store.data.mannerWeight += weight
      } else {
        let nameArr = []
        this.$store.data.mannerList.forEach((item) => {
          nameArr.push(item.indexName)
        })
        let arr = []
        let sumweight = 0
        newArr.forEach((item) => {
          if (nameArr.indexOf(item.indexName) === -1) {
            arr.push(item)
            sumweight += item.weight
          }
        })
        let fistArr = this.$store.data.mannerList.concat(arr)
        let sWeight = this.$store.data.mannerWeight += sumweight
        this.$store.data.mannerWeight = sWeight
        this.$store.data.mannerList = fistArr
        this.update()
      }
    }

    // this.$store.data.sumWeight = this.$store.data.sumWeight += weight
    this.update()
    // console.log(this.$store.data);
  }
});
